package com.agricultural.strategy.impl;

import com.agricultural.equipment.AgriculturalEquipment;
import com.agricultural.strategy.PathPlanningStrategy;

import java.util.Map;

// 湿土路径规划策略 - 当土壤湿度过高时使用
public class WetSoilPathStrategy implements PathPlanningStrategy {
    
    // 获取策略名称
    @Override
    public String getStrategyName() {
        return "湿土路径规划策略";
    }
    
    // 规划路径
    @Override
    public void planPath(AgriculturalEquipment equipment, Map<String, Object> environmentData) {
        System.out.println("[" + equipment.getType() + "-" + equipment.getId() + "] 使用湿土路径规划策略");
        
        // 检查环境数据中是否有土壤湿度信息
        double soilMoisture = 60.0; // 默认值
        if (environmentData != null && environmentData.containsKey("soilMoisture")) {
            soilMoisture = (double) environmentData.get("soilMoisture");
        }
        
        System.out.println("[" + equipment.getType() + "-" + equipment.getId() + "] 当前土壤湿度: " + soilMoisture + "%");
        
        // 模拟路径规划计算 - 避开湿地区域
        double currentX = equipment.getStatus().get("positionX") != null ? 
                          (double) equipment.getStatus().get("positionX") : 0.0;
        double currentY = equipment.getStatus().get("positionY") != null ? 
                          (double) equipment.getStatus().get("positionY") : 0.0;
        
        // 如果湿度过高，横向移动而不是继续直行
        double newX, newY;
        if (soilMoisture > 75.0) {
            System.out.println("[" + equipment.getType() + "-" + equipment.getId() + "] 土壤湿度过高，避开此区域");
            newX = currentX;
            newY = currentY + 15.0; // 横向移动
        } else {
            newX = currentX + 10.0;
            newY = currentY;
        }
        
        // 更新设备位置
        String newPosition = String.format("(%.2f, %.2f)", newX, newY);
        equipment.updateStatusData("position", newPosition);
        
        System.out.println("[" + equipment.getType() + "-" + equipment.getId() + "] 新路径规划完成: " + newPosition);
    }
}    